// AdBlockerBHO.h : Declaration of the CAdBlockerBHO

#pragma once
#include "resource.h"       // main symbols

#include <shlguid.h>     // IID_IWebBrowser2, DIID_DWebBrowserEvents2, etc.
#include <exdispid.h> // DISPID_DOCUMENTCOMPLETE, etc.

#include <Wininet.h>

#include "InsysInternetExplorerAdBlocker_i.h"

#include "AdvertBlocker.h"
#include "RegistryManager.h"

#if defined(_WIN32_WCE) && !defined(_CE_DCOM) && !defined(_CE_ALLOW_SINGLE_THREADED_OBJECTS_IN_MTA)
#error "Single-threaded COM objects are not properly supported on Windows CE platform, such as the Windows Mobile platforms that do not include full DCOM support. Define _CE_ALLOW_SINGLE_THREADED_OBJECTS_IN_MTA to force ATL to support creating single-thread COM object's and allow use of it's single-threaded COM object implementations. The threading model in your rgs file was set to 'Free' as that is the only threading model supported in non DCOM Windows CE platforms."
#endif

using namespace ATL;


// CAdBlockerBHO

class ATL_NO_VTABLE CAdBlockerBHO :
	public CComObjectRootEx<CComSingleThreadModel>,
	public CComCoClass<CAdBlockerBHO, &CLSID_AdBlockerBHO>,
	public IObjectWithSiteImpl<CAdBlockerBHO>,
	public IDispatchImpl<IAdBlockerBHO, &IID_IAdBlockerBHO, &LIBID_InsysInternetExplorerAdBlockerLib, /*wMajor =*/ 1, /*wMinor =*/ 0>,
	public IDispEventImpl<1, CAdBlockerBHO, &DIID_DWebBrowserEvents2, &LIBID_SHDocVw, 1, 1>
{

public:
	//if we need to add other IDispEventImpl then we will add
	//other typedef for easy 'advising' (like "DWebBrowserEvents2Sink::DispEventAdvise(_webBrowser)")
	//right now calling DispEventAdvise in't ambiguous so we don't need typedef but we're leaving it in case of iheriting another IDispEventImpl
	typedef IDispEventImpl<1, CAdBlockerBHO, &DIID_DWebBrowserEvents2, &LIBID_SHDocVw, 1, 1> DWebBrowserEvents2Sink;	

	CAdBlockerBHO()
	{	
		_isWebBrowserAdvised = false;
	}

DECLARE_REGISTRY_RESOURCEID(IDR_ADBLOCKERBHO)

DECLARE_NOT_AGGREGATABLE(CAdBlockerBHO)

BEGIN_COM_MAP(CAdBlockerBHO)
	COM_INTERFACE_ENTRY(IAdBlockerBHO)
	COM_INTERFACE_ENTRY(IDispatch)
	COM_INTERFACE_ENTRY(IObjectWithSite)
END_COM_MAP()

	DECLARE_PROTECT_FINAL_CONSTRUCT()

	HRESULT FinalConstruct()
	{
		return S_OK;
	}

	void FinalRelease()
	{
	}

public:
	STDMETHOD(SetSite)(IUnknown *pUnkSite);

	BEGIN_SINK_MAP(CAdBlockerBHO)
		SINK_ENTRY_EX(1, DIID_DWebBrowserEvents2, DISPID_DOCUMENTCOMPLETE, OnDocumentComplete)
		SINK_ENTRY_EX(1, DIID_DWebBrowserEvents2, DISPID_BEFORENAVIGATE2, OnBeforeNavigate2)
	END_SINK_MAP()

    // DWebBrowserEvents2
    void STDMETHODCALLTYPE OnDocumentComplete(IDispatch *pDisp, VARIANT *pvarURL); 
	void STDMETHODCALLTYPE OnBeforeNavigate2(IDispatch *pDisp, VARIANT *url, VARIANT *Flags, VARIANT *TargetFrameName, VARIANT *PostData, VARIANT *Headers, VARIANT_BOOL *Cancel);

private:
	void RegisterHttpHandler();

    bool _isWebBrowserAdvised; 	
    CComPtr<IWebBrowser2>  _webBrowser;
	CAdvertBlocker _advertBlocker;

	CRegistryManager _regMgr;
};

OBJECT_ENTRY_AUTO(__uuidof(AdBlockerBHO), CAdBlockerBHO)
